package edu.hawaii.ics414;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Initializes the profile view for looking at your favorite games.
 * 
 * @author Matthew Mizumoto
 * 
 */

public class ProfileView extends Activity {

  /*
   * Called when the activity is first created.
   */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.profile_view);
    TextView name = (TextView) findViewById(R.id.username);
    name.setText("WELCOME " + CurrentUser.username);

    String[] games;
    ArrayList<String> favorites = new ArrayList<String>();
    ListView listView = (ListView) findViewById(R.id.favoritesList);

    try {
      games = new GetFavorites().execute(CurrentUser.username).get(5, TimeUnit.SECONDS);
      if (games.length > 0) {
        for (int i = 0; i < games.length; i++) {
          favorites.add(games[i].trim());
        }
      }
      ArrayAdapter<String> myFavoritesAdapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, favorites);
      listView.setAdapter(myFavoritesAdapter);

      /*
       * Initializes what happens when the user selects a game from the favorites list.
       */
      listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          String item = ((TextView) view).getText().toString();
          Intent myIntent = new Intent(view.getContext(), GameView.class);
          myIntent.putExtra("gameName", item);
          startActivityForResult(myIntent, 0);
        }
      });
    }
    catch (Exception e) {
      Toast.makeText(this, "fail", Toast.LENGTH_LONG).show();
    }
  }
}